//
//  MCHomeVC.m
//  MCmeituan
//
//  Created by Mc on 15/12/13.
//  Copyright © 2015年 Mc. All rights reserved.
//

#import "MCHomeVC.h"
#import "MCTopView.h"
#import "MCfenleiViewController.h"
#import "MCquyuVC.h"
#import "MCfenleiData.h"
#import "MCquyuData.h"
#import "MCpaixuData.h"
#import "MCpaixuVC.h"

@interface MCHomeVC ()

@property (nonatomic, strong) MCfenleiViewController *fenlei;

@property (nonatomic, weak) UIBarButtonItem *fenleiItem;

@property (nonatomic, strong) MCquyuVC *quyu;

@property (nonatomic, weak) UIBarButtonItem *quyuItem;

@property (nonatomic, strong) MCpaixuVC *paixu;

@property (nonatomic, weak) UIBarButtonItem *paixuItem;


@end

@implementation MCHomeVC

#pragma mark -- 懒加载
-(MCfenleiViewController *)fenlei
{
    if (_fenlei == nil) {
        _fenlei = [[MCfenleiViewController alloc] init];
        _fenlei.modalPresentationStyle = UIModalPresentationPopover;
    }
    
    return _fenlei;
}

- (MCquyuVC *)quyu
{
    if (_quyu == nil) {
        _quyu = [[MCquyuVC alloc] init];
        _quyu.modalPresentationStyle = UIModalPresentationPopover;
    }
    return _quyu;
}

- (MCpaixuVC *)paixu
{
    if (_paixu == nil) {
        _paixu = [[MCpaixuVC alloc] init];
        _paixu.modalPresentationStyle = UIModalPresentationPopover;
    }
    return _paixu;
}

- (void)viewDidLoad
{

    [self setUpItem];
    
    [self NSNotification];
}

#pragma mark --设置baritem的数据
- (void)setUpItem
{

    UIBarButtonItem *logo = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_meituan_logo"] style:UIBarButtonItemStyleDone target:nil action:nil];
    logo.enabled = NO;

    MCTopView *fenleiView = [MCTopView topView];
    UIBarButtonItem *fenlei = [[UIBarButtonItem alloc] initWithCustomView:fenleiView];
    [fenleiView setTitle:@"美团"];
    [fenleiView setSubTitle:@"全部分类"];
    [fenleiView setIcon:@"icon_category_-1" andHightIcon:@"icon_category_highlighted_-1"];
    [fenleiView addTarget:self action:@selector(fenleiClick)];
    self.fenleiItem = fenlei;
    
    MCTopView *quyuView = [MCTopView topView];
    UIBarButtonItem *quyu = [[UIBarButtonItem alloc] initWithCustomView:quyuView];
    [quyuView setTitle:@"广州"];
    [quyuView setSubTitle:@"全部区域"];
    [quyuView setIcon:@"icon_district" andHightIcon:@"icon_district_highlighted"];
    [quyuView addTarget:self action:@selector(quyuClick)];
    self.quyuItem = quyu;
    
    MCTopView *paixuView = [MCTopView topView];
    UIBarButtonItem *paixu = [[UIBarButtonItem alloc] initWithCustomView:paixuView];
    [paixuView setTitle:@"排序"];
    [paixuView setSubTitle:@"默认排序"];
    [paixuView setIcon:@"icon_sort" andHightIcon:@"icon_sort_highlighted"];
    [paixuView addTarget:self action:@selector(paixuClick)];
    self.paixuItem = paixu;
    
    self.navigationItem.leftBarButtonItems = @[logo,fenlei, quyu, paixu];
    
}

#pragma mark --设置代理
- (void)NSNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fenlei:) name:@"MCfenleiNSNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(quyu:) name:@"MCquyuNSNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paixu:) name:@"MCpaixu" object:nil];
}


#pragma mark --弹出窗口
- (void)fenleiClick{
  
    self.fenlei.popoverPresentationController.barButtonItem = self.fenleiItem;
    
    [self presentViewController:self.fenlei animated:YES completion:nil];
}

- (void)quyuClick
{
    self.quyu.popoverPresentationController.barButtonItem = self.quyuItem;
    [self presentViewController:self.quyu animated:YES completion:nil];
    
}

- (void)paixuClick
{
    self.paixu.popoverPresentationController.barButtonItem = self.paixuItem;
    [self presentViewController:self.paixu animated:YES completion:nil];
}


- (void)fenlei:(NSNotification *)noti
{
    MCfenleiData *fenleiData = noti.userInfo[@"MCLeftRow"];
    NSString *str = noti.userInfo[@"MCRightRow"];
    
    MCTopView *fenleiView = self.fenleiItem.customView;
    if (fenleiData.subcategories.count == 0) {
        [fenleiView setTitle:@"美团"];
        [fenleiView setSubTitle:fenleiData.name];
    }else{
    [fenleiView setTitle:fenleiData.name];

    [fenleiView setSubTitle:str];
    }
    [fenleiView setIcon:fenleiData.icon andHightIcon:fenleiData.highlighted_icon];
    
    [self.fenlei dismissViewControllerAnimated:YES completion:nil];
}

- (void)quyu:(NSNotification *)noti
{
    MCquyuData *quyuData = noti.userInfo[@"quyu"];
    NSString *str = noti.userInfo[@"quyustr"];
    
    MCTopView *quyuView = self.quyuItem.customView;
    if (quyuData.subregions.count == 0) {
        [quyuView setTitle:@"广州"];
        [quyuView setSubTitle:quyuData.name];
    }else{
        [quyuView setTitle:quyuData.name];
        [quyuView setSubTitle:str];
    }
    
    [quyuView setIcon:@"icon_district" andHightIcon:@"icon_district_highlighted"];
    [self.quyu dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)paixu:(NSNotification *)noti
{
    MCpaixuData *data = noti.userInfo[@"paixu"];
    MCTopView *paixu = self.paixuItem.customView;
    [paixu setTitle:data.label];
    [self.paixu dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
